// poj1986
// 题意：求树上两点距离，可以离线
//
// 题解：用离线的tarjan lca做。
//
// note: 这份代码可能不能直接拿来缩点，求连通分量和求桥、割点的似乎有点
//       不一样？
//
// run: $exec < input
#include <cstdio>
#include <vector>
#include <utility>

struct node
{
	int parent, rank, ancestor, color;
};

int const maxn = 40007;
int cost_to_root[maxn];
node every[maxn];
bool vis[maxn];
int n, m, k;

typedef int index;
typedef std::pair<int, index> node_type;

std::vector<std::pair<std::pair<int, int>, int> > input_pair(maxn);
std::vector<std::vector<node_type> > common_pair;
std::vector<std::vector<std::pair<int, int> > > graph;

void make_set(int u)
{
	every[u].parent = u;
	every[u].rank = 0;
}

int find(int u)
{
	return (every[u].parent == u) ? u : (every[u].parent = find(every[u].parent));
}

void set_union(int u, int v)
{
	int uroot = find(u);
	int vroot = find(v);
	if (every[uroot].rank > every[vroot].rank)
		every[uroot].parent = vroot;
	else if (uroot != vroot) {
		every[vroot].parent = uroot;
		every[uroot].rank++;
	}
}

void tarjan_offline_lca(int u)
{
	vis[u] = true;
	make_set(u);
	every[u].ancestor = u;
	for (int i = 0; i < (int)graph[u].size(); i++) {
		int v = graph[u][i].first, c = graph[u][i].second;
		if (vis[v]) continue;
		cost_to_root[v] = cost_to_root[u] + c;
		tarjan_offline_lca(v);
		set_union(u, v);
		every[find(u)].ancestor = u;
	}
	every[u].color = 1;

	for (int i = 0; i < (int)common_pair[u].size(); i++) {
		int v = common_pair[u][i].first, index = common_pair[u][i].second;
		if (every[v].color)
			input_pair[index].second = every[find(v)].ancestor;
	}
}

int main()
{
	std::scanf("%d%d", &n, &m);
	graph.resize(n + 1);
	common_pair.resize(n + 1);
	for (int i = 0, x, y; i < m; i++) {
		std::pair<int, int> tmp;
		std::scanf("%d%d%d", &x, &y, &tmp.second);
		char ch; std::scanf(" %c", &ch);
		tmp.first = y; graph[x].push_back(tmp);
		tmp.first = x; graph[y].push_back(tmp);
	}

	std::scanf("%d", &k);
	for (int i = 0, x, y; i < k; i++) {
		std::scanf("%d%d", &x, &y);
		input_pair[i].first.first = x;
		input_pair[i].first.second = y;
		node_type tmp; tmp.second = i;
		tmp.first = y; common_pair[x].push_back(tmp);
		tmp.first = x; common_pair[y].push_back(tmp);
	}

	tarjan_offline_lca(1);

	for (int i = 0; i < k; i++) {
		int u = input_pair[i].first.first, v = input_pair[i].first.second, c = input_pair[i].second;
		int ans = cost_to_root[u] + cost_to_root[v] - 2 * cost_to_root[c];
		std::printf("%d\n", ans);
	}
}

